const { v4: uuidv4 } = require('uuid');

module.exports = {
  up: async (queryInterface) => {
    const now = new Date();

    // 检查是否已存在配置
    const [existingConfigs] = await queryInterface.sequelize.query(
      `SELECT id FROM system_configs WHERE config_key = 'file.storage.type' LIMIT 1`
    );

    if (existingConfigs && existingConfigs.length > 0) {
      console.log('✅ 系统配置已存在');
      return;
    }

    // 添加默认的文件存储配置
    const configs = [
      // 文件存储类型
      {
        id: uuidv4(),
        tenant_id: null, // 全局配置
        config_key: 'file.storage.type',
        config_value: 'minio',
        config_type: 'string',
        description: '文件存储类型（minio/local/oss）',
        is_system: true,
        created_at: now,
        updated_at: now,
      },
      // MinIO 配置
      {
        id: uuidv4(),
        tenant_id: null,
        config_key: 'file.storage.minio.endpoint',
        config_value: 'minion.xlcig.cn',
        config_type: 'string',
        description: 'MinIO 服务器地址',
        is_system: true,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        config_key: 'file.storage.minio.port',
        config_value: '443',
        config_type: 'number',
        description: 'MinIO 端口',
        is_system: true,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        config_key: 'file.storage.minio.useSSL',
        config_value: 'true',
        config_type: 'boolean',
        description: 'MinIO 是否使用 SSL',
        is_system: true,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        config_key: 'file.storage.minio.accessKey',
        config_value: 'admin',
        config_type: 'string',
        description: 'MinIO Access Key',
        is_system: true,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        config_key: 'file.storage.minio.secretKey',
        config_value: 'StrongPass123!',
        config_type: 'string',
        description: 'MinIO Secret Key',
        is_system: true,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        config_key: 'file.storage.minio.bucket',
        config_value: 'xux',
        config_type: 'string',
        description: 'MinIO 默认桶名',
        is_system: true,
        created_at: now,
        updated_at: now,
      },
      // 文件上传限制
      {
        id: uuidv4(),
        tenant_id: null,
        config_key: 'file.upload.maxSize',
        config_value: '50',
        config_type: 'number',
        description: '文件上传最大大小（MB）',
        is_system: true,
        created_at: now,
        updated_at: now,
      },
      {
        id: uuidv4(),
        tenant_id: null,
        config_key: 'file.upload.allowedTypes',
        config_value: '',
        config_type: 'string',
        description: '允许上传的文件类型（逗号分隔，空为不限制）',
        is_system: true,
        created_at: now,
        updated_at: now,
      },
    ];

    await queryInterface.bulkInsert('system_configs', configs);

    console.log('✅ 系统配置添加成功');
    console.log('   - 文件存储配置（9个）');
  },

  down: async (queryInterface) => {
    await queryInterface.bulkDelete('system_configs', {
      config_key: [
        'file.storage.type',
        'file.storage.minio.endpoint',
        'file.storage.minio.port',
        'file.storage.minio.useSSL',
        'file.storage.minio.accessKey',
        'file.storage.minio.secretKey',
        'file.storage.minio.bucket',
        'file.upload.maxSize',
        'file.upload.allowedTypes',
      ]
    }, {});
  }
};

